{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "a890f920-a085-4766-a74c-8ad3fc7d851a",
   "metadata": {},
   "source": [
    "# 第2章 使用时间序列分析进行需求预测"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "737e769f-a787-488b-8ac1-ef7d36e40efe",
   "metadata": {},
   "source": [
    "## 准备步骤：下载数据"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "57693ca6-062c-495a-92b9-a547ade50f7b",
   "metadata": {},
   "source": [
    "## 步骤1：数据预处理（在数据库/Excel中）"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b3908c16-4ffd-4a56-b3ba-31ec9a3610ef",
   "metadata": {},
   "source": [
    "## 步骤2：导入库和定义变量\n",
    "\n",
    "**与原书配套源代码不一样的是，jupterlab版本将在下面的步骤中按需导入，而不是一开头就导入全部**"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c70e5017-2185-4167-acb2-c7cf130ef5d9",
   "metadata": {},
   "source": [
    "## 步骤3：读取数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "5fd1c2dd-c75e-4a78-bb47-8d314e1728eb",
   "metadata": {},
   "outputs": [],
   "source": [
    "# list_flds = ['使用量','支出-天然气']\n",
    "list_flds = ['使用量-天然气']\n",
    "tuple_shape_list = [(8,0,3),(12,1,3)]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "b1e6048e-ddef-4328-b41f-562eb8bfeef2",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "当前fld是:使用量-天然气\n"
     ]
    }
   ],
   "source": [
    "import pandas as pd\n",
    "index_col_name= '月份'\n",
    "for fld in list_flds:\n",
    "    print('当前fld是:'+fld)\n",
    "    df_all = pd.read_csv('第2章示例1output_dataset.csv',sep='\\t')\n",
    "    dta = df_all[[index_col_name,fld]]    \n",
    "    dta_df = dta[fld]\n",
    "    # 下面一行是英文版旧代码，已经过期。\n",
    "    # dta_df.index = pd.DatetimeIndex(start='2001-01-01', end='2018-01-01', freq='MS') \n",
    "    # 因此改用下面这句新代码，详见 https://www.pypandas.cn/docs/user_guide/timeseries.html\n",
    "    dta_df.index = pd.date_range(start='2001-01-01', end='2018-01-01', freq='MS') # freq='MS' 参见https://pandas.pydata.org/pandas-docs/stable/user_guide/timeseries.html#timeseries-offset-aliases"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "9b027779-e80d-49cf-9114-231beb5d1448",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>月份</th>\n",
       "      <th>使用量-天然气</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>2001-01-01</td>\n",
       "      <td>90069.515</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2001-02-01</td>\n",
       "      <td>78687.679</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2001-03-01</td>\n",
       "      <td>82935.579</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>2001-04-01</td>\n",
       "      <td>80723.902</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>2001-05-01</td>\n",
       "      <td>83703.337</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>200</th>\n",
       "      <td>2017-09-01</td>\n",
       "      <td>62609.118</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>201</th>\n",
       "      <td>2017-10-01</td>\n",
       "      <td>58492.030</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>202</th>\n",
       "      <td>2017-11-01</td>\n",
       "      <td>48013.183</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>203</th>\n",
       "      <td>2017-12-01</td>\n",
       "      <td>50534.526</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>204</th>\n",
       "      <td>2018-01-01</td>\n",
       "      <td>45594.032</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>205 rows × 2 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "             月份    使用量-天然气\n",
       "0    2001-01-01  90069.515\n",
       "1    2001-02-01  78687.679\n",
       "2    2001-03-01  82935.579\n",
       "3    2001-04-01  80723.902\n",
       "4    2001-05-01  83703.337\n",
       "..          ...        ...\n",
       "200  2017-09-01  62609.118\n",
       "201  2017-10-01  58492.030\n",
       "202  2017-11-01  48013.183\n",
       "203  2017-12-01  50534.526\n",
       "204  2018-01-01  45594.032\n",
       "\n",
       "[205 rows x 2 columns]"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dta"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "859661f7-450e-423b-89bf-16977a184061",
   "metadata": {},
   "source": [
    "## 步骤4：数据预处理（在Python中）"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6fa74cf6-32e8-43f9-a59e-67f2d4d1ae91",
   "metadata": {},
   "source": [
    "## 步骤5：训练和验证模型"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "a0174d80-0af7-4a7e-9a1b-b27c5a6ea24c",
   "metadata": {},
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 't_i' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[1;32m~\\AppData\\Local\\Temp/ipykernel_3208/1029402657.py\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mtuple_shape\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mtuple_shape_list\u001b[0m\u001b[1;33m[\u001b[0m\u001b[0mt_i\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m      2\u001b[0m \u001b[1;32mif\u001b[0m \u001b[0mtuple_shape\u001b[0m \u001b[1;33m==\u001b[0m \u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      3\u001b[0m     \u001b[0mtuple_shape\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;33m(\u001b[0m\u001b[1;36m7\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;36m1\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;36m2\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      4\u001b[0m \u001b[0merror\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0mtemp_data\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0mlowest_model\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mforecasting\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mdta_df\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0mfld\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;32mTrue\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0mtuple_shape\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;36m0.9\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0mfld\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      5\u001b[0m \u001b[0mprint_plt\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mtemp_data\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0mfld\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mNameError\u001b[0m: name 't_i' is not defined"
     ]
    }
   ],
   "source": [
    "tuple_shape = tuple_shape_list[t_i]\n",
    "if tuple_shape == ():\n",
    "    tuple_shape = (7,1,2)\n",
    "error,temp_data,lowest_model = forecasting(dta_df,fld,True,tuple_shape,0.9,fld)\n",
    "print_plt(temp_data,fld)\n",
    "t_i+=1"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2839611e-e71d-40ba-aff3-3dc79ab8d158",
   "metadata": {},
   "source": [
    "## 步骤6：测试模型"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b94621e3-8f4b-4aaa-8346-cba558bddf81",
   "metadata": {},
   "source": [
    "## 步骤7：将测试结果可视化"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3c63bb78-8f37-4d30-8635-226b55a5873d",
   "metadata": {},
   "source": [
    "## 步骤8：生成用于生产环境的模型"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "75794498-0b1c-40b3-bfc0-32673f4d580d",
   "metadata": {},
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 'lowest_model' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[1;32m~\\AppData\\Local\\Temp/ipykernel_3208/2806318526.py\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m      1\u001b[0m \u001b[1;32mimport\u001b[0m \u001b[0mpickle\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      2\u001b[0m \u001b[0mf_ARIMA\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mopen\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mfld\u001b[0m\u001b[1;33m+\u001b[0m\u001b[1;34m'.pkl'\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;34m\"wb+\"\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 3\u001b[1;33m \u001b[0mpickle\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mdump\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mlowest_model\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mf_ARIMA\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m      4\u001b[0m \u001b[0mf_ARIMA\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mclose\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mNameError\u001b[0m: name 'lowest_model' is not defined"
     ]
    }
   ],
   "source": [
    "import pickle\n",
    "f_ARIMA=open(fld+'.pkl',\"wb+\")\n",
    "pickle.dump(lowest_model, f_ARIMA)\n",
    "f_ARIMA.close()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
